import re


# 一、 11个元字符学习
# . ^ $ * + ? [] {} | () \

# 二、 re模块常用方法
# re.findall()  以列表形式返回所有满足条件的结果
## re.findIter  返回的 结果列表的迭代器
# re.search() 返回匹配的第一个结果
# re.matc()
# re.split()
# re.sub()
# re.compile()

# . match single char

# ^ match begin with some single char

# $ match end with some single char 


# *  zero or manytimes  [0,+oo]
ret = re.findall("abc*",'abccc');
print(ret)

# +   match group() or single char 1 or manytimes  [1,+oo]
ret = re.findall("abc+",'abccc');
print(ret)

# ?  match group() or single char  0 or 1 times  [0,1]
ret = re.findall("abc?",'abccc');
print(ret)

# {} match group() or single char  spec some times  {1}  {1,4}
ret = re.findall("abc{1,4}",'abccc');
print(ret)


# []字符集合
# match range  a to z  [a-z0-9]
ret= re.findall('[a-z]','acd')
print(ret)

# match  b or c
ret = re.findall("a[bc]d",'abcd');
print(ret)

## 集合里 有用 的符号 -  ^ \
ret= re.findall('[^ab]','45bdhab"3')
print(ret)

### 转义字符 \
#### \后面跟元字符表示去除特殊功能  
#### \后面跟普通字符表示实现特殊功能  
####### \d 
####### \D 
####### \s 
####### \S 
####### \w 
####### \W 
####### \b 
# 元字符分组()

ret = re.findall('(ad)+','www.badu.cadom')
print(ret)

ret = re.findall('\w+\d{3}','it001/John')
print(ret)
## group with name
ret = re.search('(?P<id>\w{2,}\d{3,})/(?P<name>\w{3,})','it001/John')
if ret is not None:
    print(ret.group())
    print(ret.group("id"))
    print(ret.group("name"))
    print(ret)

# find all will math the group content
ret = re.findall('www.(\w+).com','www.baidu.com')
print(ret)

# match all 
ret = re.findall('www.[\w]+.com','www.baidu.com')
print(ret)

# 元字符或 |
ret = re.search('(ab)|\d','rabvhd9sd')
print(ret)
print(ret.group())

ret =re.finditer('(ab)|\d','rabvhd9sd')
print(ret)
print(next(ret).group())


# pratise: pattern  get (2+1) from (1+2*(3+2)) 
ret=re.findall('\([^()]+\)','(1+2*(3+(2+1)))')
print(ret)


# pratise: pattern  ( and ) times
